﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections ;

namespace iSun.Model
{
    /// <summary>
    /// 实体类的验证类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// 
    ///   private IEnumerable<String> Check()
    ///  {
    ///      return (new Validator<PhotoImage>(this)).validate(p => p.PhotImage.Length <= 0, "").ErrorMessage;
    ///  }
    ///
    ///

    public class Validator<TEntity> where TEntity : BaseEntity
    {
        private TEntity _entity;
        private IList<String> _erorrMessage;
        public IList<String> ErrorMessage
        {
            get
            {
                if (_erorrMessage == null)
                {
                    _erorrMessage = new List<String>();
                }
                return _erorrMessage;
            }
        }

        public Validator(TEntity entity)
        {
            _entity = entity;
        }
        public Validator<TEntity> validate(Predicate<TEntity> predicate, string errorMessage)
        {
            if (!predicate(this._entity))
            {
                this.ErrorMessage.Add(errorMessage);
            }
            return this;
        }

    }
}
